<script setup>
import api from '../api/family_api.js'
import {ref,onMounted} from "vue";
import {ElMessageBox} from "element-plus";
import lodash from 'lodash'
//定义绑定的数据
//表格中的数据
const tableData = ref([]);

const total = ref(0);
const editWinVisible = ref(false);
const addWinVisible = ref(false);
//查询的数据
const searchObj =ref( {
  current: 1,//当前页码
  limit: 10,//每页条数
  householder_id: '',//查询条件
  family_code: '',
  household_type: '',
})
const search = async () => {
  var result=await api.page(searchObj.value);//刷新数据
  tableData.value = result.data;
  total.value = result.count;
}
//要修改或者新增的数据
const editObj = ref({});
const addObj = ref({});

onMounted(async ()=>{
  search();
})
//页面加载后挂载的数据

//定义方法
const handleSizeChange = (val) => {
  search.log(searchObj)
  search();
}
const handleCurrentChange = (val) => {
  searchObj.value.current = val;
  search();
}
const editCancel=()=>{
  editObj.value = {};//清空数据
  editWinVisible.value = false;//关闭窗口
}
const editCommit=async () =>{
  await api.edit(editObj.value);//新增或修改
  search();//刷新数据
  editWinVisible.value = false;//关闭窗口
}
const addCancel=()=>{
  addObj.value = {};//清空数据
  addWinVisible.value = false;//关闭窗口
}
const addCommit=async () =>{
  await api.add(addObj.value);//新增或修改
  search();//刷新数据
  addWinVisible.value = false;//关闭窗口
  addObj.value = {};
}

const handleSearch = () => {
  searchObj.value.current = 1;
  search();
}
const handleAdd = (index, row) => {
  addWinVisible.value = true;//显示窗口
  console.log(index, row)//获取当前行数据
}

const handleEdit = (index, row) => {
  editObj.value=lodash.clone(row)//拷贝数据
  editObj.value = row;//设置要修改的数据
  editWinVisible.value = true;//显示窗口
  console.log(index, row)//获取当前行数据
}
const handleDelete = (index, row) => {
  ElMessageBox.confirm(
      '确认删掉这条数据吗？',
      '删除提示',
      {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'errpr',
      }
  )
      .then(async () => {
        api.del(row.fammily_id);
        //刷新数据
        searchObj.value.current=1;//设置从第一页开始查询
        search();
      })
      .catch(() => {

      })
}


</script>

<template>
  <!--  查询-->
  <el-form :inline="true" :model="searchObj" class="demo-form-inline" style="margin-top: 10px">
    <el-form-item label="户主ID" :label-width="formLabelWidth">
      <el-input v-model="searchObj.householder_id" autocomplete="off" />
    </el-form-item>
    <el-form-item label="家庭编号" :label-width="formLabelWidth">
      <el-input v-model="searchObj.family_code" autocomplete="off" />
    </el-form-item>
    <el-form-item label="户籍类型" :label-width="formLabelWidth">
      <el-select v-model="searchObj.household_type" placeholder="请选择户籍类型" style="width: 100px">
        <el-option label="" value="" />
        <el-option label="一般户" value="一般户" />
        <el-option label="低保户" value="低保户" />
        <el-option label="五保户" value="五保户" />
        <el-option label="脱贫户" value="脱贫户" />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleSearch">查询</el-button>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleAdd">新增</el-button>
    </el-form-item>
  </el-form>
  <!--  展示-->
  <el-table :data="tableData" style="width: 100%">
    <el-table-column label="家庭ID" width="80">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <span style="margin-left: 10px">{{ scope.row.family_id }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="户主ID" width="80">
      <template #default="scope">
        <el-popover effect="light" trigger="hover" placement="top" width="auto">
          <template #reference>
            <el-tag>{{ scope.row.householder_id }}</el-tag>
          </template>
        </el-popover>
      </template>
    </el-table-column>
    <el-table-column label="家庭编号" width="80">
      <template #default="scope">
        <el-popover effect="light" trigger="hover" placement="top" width="auto">
          <template #reference>
            <el-tag>{{ scope.row.family_code }}</el-tag>
          </template>
        </el-popover>
      </template>
    </el-table-column>
    <el-table-column label="详细住址" width="80">
      <template #default="scope">
        <el-popover effect="light" trigger="hover" placement="top" width="auto">
          <template #reference>
            <el-tag>{{ scope.row.address }}</el-tag>
          </template>
        </el-popover>
      </template>
    </el-table-column>
    <el-table-column label="户籍类型" width="80">
      <template #default="scope">
        <el-popover effect="light" trigger="hover" placement="top" width="auto">
          <template #reference>
            <el-tag>{{ scope.row.household_type }}</el-tag>
          </template>
        </el-popover>
      </template>
    </el-table-column>
    <el-table-column label="家庭成员总数" width="80">
      <template #default="scope">
        <el-popover effect="light" trigger="hover" placement="top" width="auto">
          <template #reference>
            <el-tag>{{ scope.row.total_members }}</el-tag>
          </template>
        </el-popover>
      </template>
    </el-table-column>
    <el-table-column label="创建时间" width="180">
      <template #default="scope">
        <el-popover effect="light" trigger="hover" placement="top" width="auto">
          <template #reference>
            <el-tag>{{ scope.row.create_time }}</el-tag>
          </template>
        </el-popover>
      </template>
    </el-table-column>
    <el-table-column label="数据更新" width="180">
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">
          编辑
        </el-button>
        <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
        >
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
      v-model:current-page="searchObj.current"
      v-model:page-size="searchObj.limit"
      :page-sizes="[10, 20, 30, 40]"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
  />

  <!--  编辑对话框-->
  <el-dialog v-model="editWinVisible" title="编辑" width="800">
    <el-form :model="editObj">
      <el-form-item label="户主ID" :label-width="formLabelWidth">
        <el-input v-model="editObj.householder_id" autocomplete="off" />
      </el-form-item>
      <el-form-item label="家庭编号" :label-width="formLabelWidth">
        <el-input v-model="editObj.family_code" autocomplete="off" />
      </el-form-item>
      <el-form-item label="详细住址" :label-width="formLabelWidth">
        <el-input v-model="editObj.address" autocomplete="off" />
      </el-form-item>
      <el-form-item label="户籍类型" :label-width="formLabelWidth">
        <el-select v-model="editObj.household_type" placeholder="请选择安全等级" style="width: 100%">
          <el-option label="一般户" value="一般户" />
          <el-option label="低保户" value="低保户" />
          <el-option label="五保户" value="五保户" />
          <el-option label="脱贫户" value="脱贫户" />
        </el-select>
      </el-form-item>
      <el-form-item label="家庭成员总数" :label-width="formLabelWidth">
        <el-input v-model="editObj.total_members" autocomplete="off" />
      </el-form-item>
      <el-form-item label="创建时间" :label-width="formLabelWidth">
        <el-input v-model="editObj.create_time" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="editCancel">取消</el-button>
        <el-button type="primary" @click="editCommit">
          提交
        </el-button>
      </div>
    </template>
  </el-dialog>

  <el-dialog v-model="addWinVisible" title="新增" width="800">
    <el-form :model="addObj">
      <<el-form-item label="户主ID" :label-width="formLabelWidth">
      <el-input v-model="addObj.householder_id" autocomplete="off" />
    </el-form-item>
      <el-form-item label="家庭编号" :label-width="formLabelWidth">
        <el-input v-model="addObj.family_code" autocomplete="off" />
      </el-form-item>
      <el-form-item label="详细住址" :label-width="formLabelWidth">
        <el-input v-model="addObj.address" autocomplete="off" />
      </el-form-item>
      <el-form-item label="户籍类型" :label-width="formLabelWidth">
        <el-select v-model="addObj.household_type" placeholder="请选择安全等级" style="width: 100%">
          <el-option label="一般户" value="一般户" />
          <el-option label="低保户" value="低保户" />
          <el-option label="五保户" value="五保户" />
          <el-option label="脱贫户" value="脱贫户" />
        </el-select>
      </el-form-item>
      <el-form-item label="家庭成员总数" :label-width="formLabelWidth">
        <el-input v-model="addObj.total_members" autocomplete="off" />
      </el-form-item>
      <el-form-item label="创建时间" :label-width="formLabelWidth">
        <el-input v-model="addObj.create_time" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="addCancel">取消</el-button>
        <el-button type="primary" @click="addCommit">
          提交
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style></style>